/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

/* 
 * File:   CSwitchGui.cpp
 * Author: Administrator
 * 
 * Created on 2018年1月18日, 下午4:38
 */

#include "CSwitchGui.h"
#if USE_GTK_WINDOW
#include "gtk/gtk.h"
#include "PublicLog.h"
#include "CSwitchManager.h"
//GTK相关
void destroy(GtkWidget*widget,gpointer data){
    gtk_main_quit ();
}
void select_switch_row_callback(GtkWidget*widget,gint row,gint column,GdkEventButton *event,gpointer data){
	printf("row:%d event:%d\n",row,event->type);
	if(event->type==GDK_2BUTTON_PRESS){
		char*tmp=NULL;
		gtk_clist_get_text(GTK_CLIST(widget),row,0,&tmp);
		printf("IP:%s\n",tmp);
		((CSwitchGui*)data)->TryFindifWindow(tmp);
	}
}
void select_port_row_callback(GtkWidget*widget,gint row,gint column,GdkEventButton *event,gpointer data){
	printf("row:%d event:%d\n",row,event->type);
	if(event->type==GDK_2BUTTON_PRESS){
		
	}
}
void CSwitchGui::InitWindow(){
	int argc=0;
	char** argv=NULL;
	gtk_init(&argc,&argv);
	GtkWidget*window_main=gtk_window_new(GTK_WINDOW_TOPLEVEL);
	gtk_window_set_title(GTK_WINDOW(window_main),"交换机管理系统");
	gtk_window_set_default_size(GTK_WINDOW(window_main),640,480);
	g_signal_connect (G_OBJECT(window_main),"destroy",GTK_SIGNAL_FUNC(gtk_main_quit),NULL);
	char*titles[4]={"IP","sysName","ifNumber","sysDescr"};
	GtkWidget*switchlist=gtk_clist_new_with_titles(4,titles);
	
	gtk_clist_set_column_width(GTK_CLIST(switchlist),0,100);
	gtk_clist_set_column_width(GTK_CLIST(switchlist),1,200);
	
	GtkWidget *vbox=gtk_vbox_new(FALSE, 5);
	gtk_container_set_border_width(GTK_CONTAINER(vbox), 5);
	gtk_container_add(GTK_CONTAINER(window_main), vbox);
	
	GtkWidget *scrolled_window=gtk_scrolled_window_new (NULL, NULL);
	gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
	gtk_container_add(GTK_CONTAINER(scrolled_window), switchlist);
	
	GtkWidget *button_add,*button_clear,*button_hide_show;
	GtkWidget*hbox=gtk_hbox_new(FALSE, 0);
	gtk_box_pack_start(GTK_BOX(vbox),hbox,FALSE,TRUE,0);
	
	button_add = gtk_button_new_with_label("Add List");
	button_clear = gtk_button_new_with_label("Clear List");
	button_hide_show = gtk_button_new_with_label("Hide/Show titles");
	gtk_box_pack_start(GTK_BOX(hbox), button_add, TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(hbox), button_clear, TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(hbox), button_hide_show, TRUE, TRUE, 0);
	
	g_signal_connect (G_OBJECT(switchlist),"select_row",GTK_SIGNAL_FUNC(select_switch_row_callback),this);
	//gtk_widget_show (m_window);
	ReFlashSwitchList(switchlist);
	gtk_widget_show_all(window_main);	
}
CSwitchGui::CSwitchGui(){
}

CSwitchGui::~CSwitchGui(){
}

void CSwitchGui::RunMain(){
	InitWindow();
	gtk_main();
}

void CSwitchGui::TryFindifWindow(const char*IP){
	GList*toplist= gtk_window_list_toplevels();
	GList*pos=toplist;
	char title[256]={0};
	sprintf(title,"%s-端口表",IP);
	while(pos!=NULL){
		const char*tmp=gtk_window_get_title((GtkWindow*)pos->data);
		if((tmp!=NULL)&&(strcmp(tmp,title)==0)){
			break;
		}
		pos=pos->next;
	}
	if(pos==NULL){
		CreateifWindow(IP);
	}else{		
		//gtk_window_set_keep_below(GTK_WINDOW((GtkWidget*)pos->data),TRUE);
		gtk_window_set_keep_above(GTK_WINDOW((GtkWidget*)pos->data),TRUE);  
		INFO("FindifWindow:%s",IP);
		gtk_window_set_keep_above(GTK_WINDOW((GtkWidget*)pos->data),FALSE);  
	}
	g_list_free(toplist);
}
void CSwitchGui::CreateifWindow(const char*IP){
	INFO("CreateifWindow:%s",IP);
	GtkWidget *newwin=gtk_window_new(GTK_WINDOW_TOPLEVEL);
	char title[256]={0};
	sprintf(title,"%s-端口表",IP);
	gtk_window_set_title(GTK_WINDOW(newwin),title);
	gtk_window_set_default_size(GTK_WINDOW(newwin),640,480);
	
	char*titles[4]={"ifIndex","ifDescr","ifAlias","ifPhyaddress"};
	GtkWidget*portlist=gtk_clist_new_with_titles(4,titles);
	
	gtk_clist_set_column_width(GTK_CLIST(portlist),0,100);
	gtk_clist_set_column_width(GTK_CLIST(portlist),1,200);
	
	GtkWidget *vbox=gtk_vbox_new(FALSE, 5);
	gtk_container_set_border_width(GTK_CONTAINER(vbox), 5);
	gtk_container_add(GTK_CONTAINER(newwin), vbox);
	
	GtkWidget *scrolled_window=gtk_scrolled_window_new (NULL, NULL);
	gtk_box_pack_start(GTK_BOX(vbox), scrolled_window, TRUE, TRUE, 0);
	gtk_container_add(GTK_CONTAINER(scrolled_window), portlist);
	
	GtkWidget *button_add,*button_clear,*button_hide_show;
	GtkWidget*hbox=gtk_hbox_new(FALSE, 0);
	gtk_box_pack_start(GTK_BOX(vbox),hbox,FALSE,TRUE,0);
	
	button_add = gtk_button_new_with_label("Add List");
	button_clear = gtk_button_new_with_label("Clear List");
	button_hide_show = gtk_button_new_with_label("Hide/Show titles");
	gtk_box_pack_start(GTK_BOX(hbox), button_add, TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(hbox), button_clear, TRUE, TRUE, 0);
	gtk_box_pack_start(GTK_BOX(hbox), button_hide_show, TRUE, TRUE, 0);
	
	g_signal_connect (G_OBJECT(portlist),"select_row",GTK_SIGNAL_FUNC(select_port_row_callback),this);
	
	ReFlashSwitchPoerList(portlist,IP);
	gtk_widget_show_all(newwin);
}
void CSwitchGui::ReFlashSwitchList(void*widget){
	MAPSNMPSYSPACKET result=CSwitchManager::GetAllSwitchInfo();
	MAPSNMPSYSPACKET::iterator it=result.begin();
	while(it!=result.end()){
		char*datas[4];
		memset(datas,0,sizeof(datas));
		datas[0]=strdup(it->first.c_str());
		datas[1]=strdup(it->second.sysName);
		//datas[3]=strdup(it->second.sysDescr);
		gtk_clist_append(GTK_CLIST((GtkWidget*)widget),datas);
		it++;
	}
}
void CSwitchGui::ReFlashSwitchPoerList(void*widget,const char*IP){
	MAPIFTABLEINFO result=CSwitchifTable::GetAllSwitchPortList(IP);
	MAPIFTABLEINFO::iterator it=result.begin();
	while(it!=result.end()){
		char*datas[4];
		memset(datas,0,sizeof(datas));
		datas[0]=(char*)malloc(32);
		sprintf(datas[0],"%u",it->first);
		datas[1]=strdup(it->second.ifDescr);
		datas[2]=strdup(it->second.ifAlias);
		datas[3]=strdup(it->second.ifPhysAddress);
		gtk_clist_append(GTK_CLIST((GtkWidget*)widget),datas);
		it++;
	}
	
}
#endif

